package com.xidian.zhiancloud.service.equipFacility;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.*;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.model.*;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.equipFacility.PreUnitQO;
import com.xidian.zhiancloud.qo.equipFacility.PuInAndOutQO;
import com.xidian.zhiancloud.vo.emergencyManagement.WarehousingVO;
import com.xidian.zhiancloud.vo.equipFacility.DeviceTypeVO;
import com.xidian.zhiancloud.vo.equipFacility.PreUnitVO;
import com.xidian.zhiancloud.vo.equipFacility.PuInAndOutVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 备品备件设置
 * @Author: 任启亮
 * @Date: 2020/11/12
 */
@Service
public class PreUnitService extends ServiceImpl<PreUnitMapper, PreUnit> implements IService<PreUnit> {

    @Autowired
    private PreUnitMapper preUnitMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private PreUnitTypeMapper preUnitTypeMapper;
    @Autowired
    private PuInMapper puInMapper;
    @Autowired
    private PuOutMapper puOutMapper;

    public IPage<PreUnitVO> getListByQO(PreUnitQO preUnitQO) {
        Page<PreUnitVO> page = new Page<>(preUnitQO.getCurrent(), preUnitQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("add_time").setAsc(false);
        page.getOrders().add(orderItem);
        LambdaQueryWrapper<PreUnit> lambdaQueryWrapper = Wrappers.<PreUnit>lambdaQuery();
        lambdaQueryWrapper.eq(preUnitQO.getCompanyId() != null, PreUnit::getCompanyId, preUnitQO.getCompanyId())
                .eq(preUnitQO.getMngCtrDeptId() != null, PreUnit::getMngCtrDeptId, preUnitQO.getMngCtrDeptId())
                .eq(preUnitQO.getCreatorId() != null, PreUnit::getCreatorId, preUnitQO.getCreatorId())
                .eq(preUnitQO.getPreUnitTypeId() != null, PreUnit::getPreUnitTypeId, preUnitQO.getPreUnitTypeId());
        IPage<PreUnitVO> tmpAns = preUnitMapper.selectByQO(page, preUnitQO);
        return tmpAns;
    }

    public List<PuInAndOutVO> getPuInAndOut(Long id) {//List<String>
        LambdaQueryWrapper<PuIn> queryWrapper=Wrappers.lambdaQuery();
        queryWrapper.eq(PuIn::getPuId,id);
        List<PuIn> list=puInMapper.selectList(queryWrapper);
        List<PuInAndOutVO> ans=new LinkedList<>();
        list.forEach(t -> {
            PuInAndOutVO vo = new PuInAndOutVO();
            vo.setPuId(t.getPuId());
            //vo.setPuInOrOut("入库");
            vo.setCreatTime(t.getRuTime());
            PreUnit preUnit=preUnitMapper.selectById(t.getPuId());
            Organization organization=organizationMapper.selectById(preUnit.getMngCtrDeptId());
            vo.setOrganizationName(organization==null?"该部门不存在":organization.getName());
            //vo.setNum(t.getRuNum());
            String num=String.valueOf(t.getRuNum());
            //vo.setSupplier(t.getSupplier());
            vo.setPuInOrOut("入库"+num+"台,"+"供应商:"+t.getSupplier());
            ans.add(vo);
        });
        LambdaQueryWrapper<PuOut> queryWrapper1=Wrappers.lambdaQuery();
        queryWrapper1.eq(PuOut::getPuId,id);
        List<PuOut> list1=puOutMapper.selectList(queryWrapper1);
        list1.forEach(t -> {
            PuInAndOutVO vo = new PuInAndOutVO();
            vo.setPuId(t.getPuId());
            //vo.setPuInOrOut("出库");
            vo.setCreatTime(t.getChuTime());
            Organization organization=organizationMapper.selectById(t.getReceivingDeptId());
            vo.setOrganizationName(organization==null?"该部门不存在":organization.getName());
            //vo.setNum(t.getReceivingNum());
            String num=String.valueOf(t.getReceivingNum());
            SysUser user=sysUserMapper.selectById(t.getRecipientId());
            //vo.setRecipientName(user==null?"该用户已删除":user.getName());
            String username=user==null?"该用户已删除":user.getName();
            vo.setPuInOrOut(username+"领取"+num+"台");
            ans.add(vo);
        });
        ListSort(ans);
        return ans;
    }

    private static void ListSort(List<PuInAndOutVO> list) {
        //用Collections这个工具类传list进来排序
        Collections.sort(list, new Comparator<PuInAndOutVO>() {
            @Override
            public int compare(PuInAndOutVO o1, PuInAndOutVO o2) {
                //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    Date dt1 = o1.getCreatTime();
                    Date dt2 = o2.getCreatTime();
                    if (dt1.getTime() > dt2.getTime()) {
                        return 1;//小的放前面
                    }else {
                        return -1;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
    }
}

